package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.yang.entity.*;
import com.sanxiau.dormitory.yang.mapper.AttendanceMapper;
import com.sanxiau.dormitory.yang.mapper.Ex_dorMapper;
import com.sanxiau.dormitory.yang.service.AttendanceService;
import com.sanxiau.dormitory.yang.service.Ex_dorService;
import com.sanxiau.dormitory.yang.service.LeaveService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/ex_dor")
public class Ex_dorController {

    @Autowired
    Ex_dorService ex_dorService;
    @Autowired
    AttendanceService attendanceService;
    @Autowired
    Ex_dorMapper ex_dorMapper;
    @Autowired
    AttendanceMapper attendanceMapper;

    @RequestMapping("/showEx")
    public String  showEx(Model model,HttpSession session) {
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        Riches riches = ex_dorMapper.queryRichesByStu_id(session.getAttribute("userName")+"");
        //System.out.println(riches);
        Ex_dor ex_dor = new Ex_dor();
        ex_dor.setStu_name(riches.getStu_name());
        ex_dor.setStu_id(riches.getStu_id());
        ex_dor.setOld_hou_id(riches.getHou_id());
        ex_dor.setOld_dor_id(riches.getDor_id());
        ex_dor.setOld_bed_id(riches.getBed_id());
        model.addAttribute("ex_dor",ex_dor);
        return "yang/Ex_dor";
    }
    @ResponseBody
    @RequestMapping("/showBed")
    public List<String>  showBed(String hou,String dor,Model model) {
        List<String> beds = ex_dorService.showBed(hou,dor);
        return beds;
    }

    @RequestMapping("/addEx")
    public String  addEx(Ex_dor ex_dor,Model model, HttpSession session) {
        ResultInfo resultInfo = ex_dorService.addEx_dor(ex_dor,session);
        model.addAttribute("message",resultInfo.getMessage());
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        return "yang/Ex_dor";

    }
    @RequestMapping("/showEx_dors")
    public String showEx_dors(HttpSession session,Model model){
        List<Ex_dor> ex_dors = ex_dorService.showEx_dor(session);
        model.addAttribute("ex_dors",ex_dors);
        model.addAttribute("old_id",session.getAttribute("userName")+"");
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        model.addAttribute("rank",loginIn.getRemark());
        return "yang/ex_dorRecord";
    }
    @ResponseBody
    @RequestMapping("passEx_dors")
    public ResultInfo  passEx_dors(String ids,HttpSession session){
        String [] strings = ids.split(",");
        int [] ints = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ints[i] = Integer.parseInt(strings[i]);
        }
        ResultInfo resultInfo = ex_dorService.passEx_dors(ints,session);
        return resultInfo;
    }
    @ResponseBody
    @RequestMapping("overEx_dors")
    public ResultInfo  overEx_dors(String ids,HttpSession session){
        String [] strings = ids.split(",");
        int [] ints = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ints[i] = Integer.parseInt(strings[i]);
        }
        ResultInfo resultInfo = ex_dorService.overEx_dors(ints,session);
        return resultInfo;
    }
}
